package com.xinqi.modules.sys.dict.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.dict.domain.DictGradeSubjectEntity;
import com.xinqi.modules.sys.dict.dto.req.DictGradeSubjectQueryDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictGradeSubjectResultDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictSubjectResultDTO;
import com.xinqi.modules.sys.dict.manager.DictGradeSubjectManager;
import com.xinqi.modules.sys.dict.mapper.DictGradeSubjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 字典-年级学科管理层实现
 *
 * @author: linkai
 * @date: 2023/05/03
 */
@Service
@RequiredArgsConstructor
public class DictGradeSubjectManagerImpl extends BaseServiceImpl<DictGradeSubjectMapper, DictGradeSubjectEntity> implements DictGradeSubjectManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<DictGradeSubjectResultDTO> findPage(Page<Object> page, DictGradeSubjectQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<DictGradeSubjectResultDTO> findList(DictGradeSubjectQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 字典-年级学科数据
     */
    @Override
    public DictGradeSubjectEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 字典-年级学科数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public List<DictSubjectResultDTO> findSubjectByGradeId(Long gradeId) {
        return this.baseMapper.findSubjectByGradeId(gradeId);
    }

    @Override
    public List<DictSubjectResultDTO> findSubjectByStageId(Long stageId) {
        return this.baseMapper.findSubjectByStageId(stageId);
    }

    @Override
    public List<DictSubjectResultDTO> findSubjectByVersionId(Long versionId) {
        return this.baseMapper.findSubjectByVersionId(versionId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addSubject(Long gradeId, List<Long> subjectIdList) {
        // 删除之前
        delSubject(gradeId);
        // 再添加
        if (CollectionUtils.isEmpty(subjectIdList)) {
            return true;
        }
        List<DictGradeSubjectEntity> list = subjectIdList.stream().map(v -> {
            DictGradeSubjectEntity entity = new DictGradeSubjectEntity();
            entity.setGradeId(gradeId);
            entity.setSubjectId(v);
            return entity;
        }).collect(Collectors.toList());
        return this.saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delSubject(Long gradeId) {
        LambdaQueryWrapper<DictGradeSubjectEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictGradeSubjectEntity::getGradeId, gradeId);
        return this.baseMapper.delete(wrapper) > 0;
    }

}
